<?ob_start();?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<style>
			html, body {
				width: 100%;
				height: 100%;
				background: black;
				font-family: Tahoma, arial, sans-serif;
				font-size: 11px;
				line-height: 15px;
				color: white;
			}
			hr {
				margin-bottom: 30px;
			}
		</style>
		<script type="text/javascript" src="js/jquery.js"></script>
		<script>			
			$(document).ready(function(){
				setTimeout(function()
				{
					window.location = window.location;
				}, 5000)
			});
		</script
	</head>	
	<body>	
<?
	// load dom-php lib and parse site data
	ini_set('error_reporting', E_ALL);
	ini_set('display_errors', 1);

	set_time_limit(3600);

	include('lib/simple_html_dom/simple_html_dom.php');
	include('lib/parse_section.php');
	include('lib/parse_catalog.php');
	include('lib/parse_year.php');
	include('lib/parse_model.php');
	include('lib/parse_group.php');
	include('lib/parse_parts_group.php');

	$root = 'http://www.vmspark.ru';
	$url  = 'http://www.vmspark.ru/fiches';
	
	define('BR', '<br/>');

	function console($message, $color = 'white', $tab = 0)
	{		
		echo '<div style="margin-left: '.(10*$tab).'px"><font style="color: '.$color.'">'.date('H:i:s', time()).' '.$message.'</font></div>';		
	}

	// db_cfg
	$db_host	= 'localhost';
	$db_user	= 'root';
	$db_pass	= 'kscsq';
	$db_schema	= 'moto_parser';

	$link = mysql_connect($db_host, $db_user, $db_pass);
	mysql_select_db($db_schema);
	mysql_query('SET NAMES UTF8');

	$max_part	= 2000;	
	$cur_part	= 0;

	// $test = $sections[0]['catalogs'][0];
	// $test = parse_catalog($root.$test['href']);
	// $test = $test[0];
	// $test = parse_year($root.$test['href']);
	// $test = $test[0];
	// $test = parse_model($root.$test['href']);
	
	// foreach ($test as $t) {
	// 	$tmp = parse_group($root.$t['href']);
	// }	

	$sections = parse_section($url);

	foreach ($sections as $section) {
		$catalogs = $section['catalogs'];

		// isset and parsing testing;
		$query = "SELECT id, parse FROM section WHERE name = '".addslashes($section['name'])."'";
		$result   = mysql_query($query);
		if (mysql_num_rows($result))
		{
			$section_id = mysql_result($result, 0, 'id');
			console('section '.$section['name'].' already in db');

			// if section already parsed...
			if (mysql_result($result, 0, 'parse') == 1)
			{
				console('section '.$section['name'].' already parsed. Continue...<hr>', 'green');				
				continue;
			}
		}
		else
		{
			$query = 'INSERT INTO section SET name = "'.addslashes($section['name']).'", parse = 0';
			mysql_query($query);
			$section_id = mysql_insert_id();
			console('section '.$section['name'].' insert to db');
		}
		
		console('section '.$section['name'].' start parsing...');
		
		foreach ($catalogs as $catalog) {			
			$query = "SELECT id, parse FROM catalog WHERE name = '".addslashes($catalog['name'])."' AND section = ".$section_id;
			$result = mysql_query($query);
			if (mysql_num_rows($result))
			{
				$catalog_id = mysql_result($result, 0, 'id');
				console('catalog '.$catalog['name'].' already in db', 'white', 1);

				// if catalog already parsed
				if (mysql_result($result, 0, 'parse') == 1)
				{
					console('catalog '.$catalog['name'].' already parsed. Continue...', 'green', 1);				
					continue;
				}
			}
			else
			{
				$query = "INSERT INTO catalog SET name = '".addslashes($catalog['name'])."', href = '".$catalog['href']."', parse = 0, section = ".$section_id;
				mysql_query($query);
				$catalog_id = mysql_insert_id();
				console('catalog '.$catalog['name'].' insert to db', 'white', 1);
			}
			console('catalog '.$catalog['name'].'::'.$catalog_id.' start parsing...', 'yellow', 1);

			$years = parse_year($root.$catalog['href']);

			if (!is_array($years))
			{
				console('error parse: '.$root.$catalog['href'], 'red', 1);				
				continue;
			}
			foreach ($years as $year) {				
				// parse years 

				$query = "SELECT id, parse FROM year WHERE name = '".addslashes($year['name'])."' AND catalog = ".$catalog_id;				
				$result = mysql_query($query);
				if (mysql_num_rows($result))
				{
					$year_id = mysql_result($result, 0, 'id');
					console('year '.$year['name'].'::'.$year_id.' already in db', 'white', 2);

					if (mysql_result($result, 0, 'parse') == 1)
					{
						console('year '.$year['name'].' already parsed. Continue...', 'green', 2);				
						continue;
					}
				}
				else
				{
					$query = "INSERT INTO year SET name = '".addslashes($year['name'])."', parse = 0, catalog = ".$catalog_id;
					mysql_query($query);
					$year_id = mysql_insert_id();
					console('year '.$year['name'].'::'.$year_id.' insert to db', 'white', 2);
				}

				$models = parse_model($root.$year['href']);

				if (!is_array($models))
				{
					console('error parse: '.$root.$year['href'], 'red', 2);					
					continue;
				}
				foreach ($models as $model) {					
					$query = "SELECT id, parse FROM model WHERE name = '".addslashes($model['name'])."' AND year = ".$year_id;
					$result = mysql_query($query);
					if (mysql_num_rows($result))
					{
						$model_id = mysql_result($result, 0, 'id');
						console('model '.$model['name'].'::'.$model_id.' already in db', 'white', 3);

						if (mysql_result($result, 0, 'parse') == 1)
						{
							console('model '.$model['name'].' already parsed. Continue...', 'green', 3);				
							continue;
						}
					}
					else
					{
						$query = "INSERT INTO model SET name = '".addslashes($model['name'])."', parse = 0, year = ".$year_id;
						mysql_query($query);
						$model_id = mysql_insert_id();
						console('model '.$model['name'].'::'.$model_id.' insert to db', 'white', 3);
					}
					
					$parts_groups = parse_parts_group($root.$model['href']);
					foreach ($parts_groups as $parts_group) {
						$query = "SELECT id, parse FROM parts_group WHERE name = '".addslashes($parts_group['name'])."' AND model = ".$model_id;
						$result = mysql_query($query);
						if (mysql_num_rows($result))
						{
							$parts_group_id = mysql_result($result, 0, 'id');
							console('parts_group '.$parts_group['name'].'::'.$parts_group_id.' already in db', 'white', 4);

							if (mysql_result($result, 0, 'parse') == 1)
							{
								console('parts_group '.$parts_group['name'].' already parsed. Continue...', 'green', 4);				
								continue;
							}
						}
						else
						{
							$query = "INSERT INTO parts_group SET name = '".addslashes($parts_group['name'])."', parse = 0, model = ".$model_id;
							mysql_query($query);
							$parts_group_id = mysql_insert_id();
							console('parts_group '.$parts_group['name'].'::'.$parts_group_id.' insert to db', 'white', 4);
						}
						$parts = parse_group($root.$parts_group['href']);
						
						// update img file
						$query = "UPDATE parts_group SET `img` = '".$parts['img']."' WHERE id = ".$parts_group_id;
						mysql_query($query);

						foreach ($parts['p'] as $part) {							
							$query = "SELECT id, parse FROM part WHERE name = '".addslashes($part['name'])."' AND num = '".addslashes($part['num'])."' AND pos = '".addslashes($part['pos'])."' AND parts_group = ".$parts_group_id;
							$result = mysql_query($query);
							if (mysql_num_rows($result))
							{
								$part_id = mysql_result($result, 0, 'id');
								console('part '.$part['name'].'::'.$part_id.' already in db', 'gray', 5);

								if (mysql_result($result, 0, 'parse') == 1)
								{
									console('part '.$part['name'].' already parsed. Continue...', 'green', 5);				
									continue;
								}
							}
							else
							{
								$query = "INSERT INTO part SET 
											name = '".addslashes($part['name'])."', 
											pos = '".addslashes($part['pos'])."', 
											num = '".addslashes($part['num'])."', 
											parse = 1, 
											parts_group = ".$parts_group_id;
								mysql_query($query);
								$part_id = mysql_insert_id();
								console('part '.$part['name'].'::'.$part_id.' insert to db', 'gray', 5);
								$cur_part++;
								if ($cur_part>$max_part)
								{
									//header('Location: http://moto-parser.loc/parser.php');
									exit;
								}
							}							
						}
						$query = "UPDATE parts_group SET parse = 1 WHERE id = ".$parts_group_id;
						mysql_query($query);
					}
					$query = "UPDATE model SET parse = 1 WHERE id = ".$model_id;
					mysql_query($query);
				}
				$query = "UPDATE year SET parse = 1 WHERE id = ".$year_id;
				mysql_query($query);
			}
			$query = "UPDATE catalog SET parse = 1 WHERE id = ".$catalog_id;
			mysql_query($query);
		}
		$query = "UPDATE section SET parse = 1 WHERE id = ".$section_id;
		mysql_query($query);
		console('go to next section... <hr>');	
	}





